package neusoft.yuejian_11.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;

import java.util.List;

import neusoft.yuejian_11.adapter.PostListAdapter;
import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.PostBean;
import neusoft.yuejian_11.fragment.SpaceItemDecoration;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

//我的晒图列表（我的）（已改）
public class MypostActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView recyclerView;
    private PostListAdapter adapter;
    private ImageButton imageButtonBack;
    private ImageButton imageButtomAddpost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypost);
        initView();
        initData();

        //返回前一页
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //跳转到发帖页
        imageButtomAddpost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(MypostActivity.this,AddPostActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });
    }

    private void initData() {
        UserModel userModel = new UserModel();
        userModel.getMyPost(getU_id(),this);
    }

    private void initView() {
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        imageButtomAddpost= (ImageButton) findViewById(R.id.imageButtom_addpost);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        List<PostBean> postBean=(List<PostBean>)o;
        adapter = new PostListAdapter(MypostActivity.this,postBean);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(MypostActivity.this, 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new SpaceItemDecoration(8));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onFail() {

    }

}
